# TODO: 导入必要的库和模块
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_digits
import pickle

# TODO: 加载数字数据集
digits = load_digits()
X = digits.data
y = digits.target

# TODO: 将数据集划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# TODO: 初始化变量以存储最佳准确率，相应的k值和最佳knn模型
best_accuracy = 0
best_k = 0
best_knn_model = None
accuracies = []

# TODO: 尝试从1到40的k值，对于每个k值，训练knn模型，保存最佳准确率，k值和knn模型
for k in range(1, 41):
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)  # 记录每个 k 的准确率
    
    if accuracy > best_accuracy:
        best_accuracy = accuracy
        best_k = k
        best_knn_model = knn

# 打印最佳准确率和相应的 k 值
print(f"最佳准确率: {best_accuracy:.4f}")
print(f"最佳 k 值: {best_k}")

# TODO: 绘制准确率随 k 值变化的图，并标记最优 k 值
k_values = np.arange(1, 41)

plt.figure(figsize=(10, 6))
# 绘制折线图
plt.plot(k_values, accuracies, marker='o', label='Accuracy vs k')
# 绘制最优 k 值的垂直线
plt.axvline(x=best_k, color='red', linestyle='--', label=f'Best k = {best_k}')
# 在图中标注最佳 k 值和准确率
plt.text(best_k, best_accuracy, f'k={best_k}\naccuracy={best_accuracy:.4f}', 
         horizontalalignment='right', verticalalignment='bottom', color='red', fontsize=12)

# 添加标题和标签
plt.title('Accuracy vs k for KNN')
plt.xlabel('k value')
plt.ylabel('Accuracy')
plt.legend()

# TODO: 保存图像为 PDF
plt.savefig('accuracy_plot.pdf')

# 展示图像
plt.show()

# TODO: 将最佳KNN模型保存到二进制文件
with open('best_knn_model.pkl', 'wb') as f:
    pickle.dump(best_knn_model, f)


